const baseURL = 'http://rap2api.taobao.org/app/mock/297500'

// 类似于 axios 二次封装
const request = ({url, method = 'GET', data, header = {}} = {}) => {
  
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseURL + url,
      method,
      data,
      header: {
        'X-Token': 'asdfljkasdflkjasldfkjasdlkfjasdklf',
        ...header
      },
      success: resData => {
        // 响应拦截
        if (resData.statusCode === 200) {
          const {status, data} = resData.data
          if (status === 0) {
            resolve(data)
            return
          }
        }
        const err = new Error('请求出现异常')
        err.error = resData
        reject(err)
      },
      fail: err => reject(err)
    })
  })
}

module.exports = request
